<?php

class GoalsController extends TickGoalsController
{
	
	public function actionRemoveList($listId=NULL,$ref=NULL){
		$list = Lists::model()->findByPk(outBin($listId));
		if(!empty($list)){
			$list->delete();
			Goal::model()->deleteAllByAttributes(array(
				'list_id'=>outBin($listId)
			));
			if($ref!=NULL){
				Yii::app()->user->setFlash('success', 'Deleted list successfully.');
				$this->redirect(array('/goals'));
			}
		}
	}
	public function actionRemoveGoal($goalId=NULL){
		$list = Goal::model()->findByPk(outBin($goalId));
		if(!empty($list)){
			$list->delete();
		}
	}
	public function actionEdit($gId=NULL,$listId=NULL){
		$model = Goal::model()->findByPk(outBin($gId));
		if(isset($_POST['ajax']) && $_POST['ajax']==='goal-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
		if(isset($_POST['Goal']))
		{
			$model->attributes=$_POST['Goal'];
			$model->time = time();			
			if($model->validate()){

				if($model->save()){
					Yii::app()->user->setFlash('success', 'Edit goal successfully.');
					$this->redirect(Yii::app()->createUrl('/goals?uId'.outStr(currentUser()->id)));
				}else{
					$errors = $model->getErrors();
					list ($field, $_errors) = each ($errors);
					throw new Exception($_errors[0]);
				}
			}else{
				$errors = $model->getErrors();
				list ($field, $_errors) = each ($errors);
				throw new Exception($_errors[0]);
			}
		}
		$this->render('edit',array(
			
			'model'=>$model,
			
		));
	}
	public function actionAdd($listId=NULL){
		$list = Lists::model()->findByPk(outBin($listId));
		$model = new Goal;
		if(isset($_POST['ajax']) && $_POST['ajax']==='goal-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
		if(isset($_POST['Goal']))
		{
			$model->attributes=$_POST['Goal'];
			$model->time = time();
			$model->list_id = outBin($listId);
			if($model->validate()){

				if($model->save()){
					Yii::app()->user->setFlash('success', 'Add goal successfully.');
					$this->redirect(Yii::app()->createUrl('/goals/add',array('listId'=>$listId)));
				}else{
					$errors = $model->getErrors();
					list ($field, $_errors) = each ($errors);
					throw new Exception($_errors[0]);
				}
			}else{
				$errors = $model->getErrors();
				list ($field, $_errors) = each ($errors);
				throw new Exception($_errors[0]);
			}
		}
		$dataGoals  = Goal::model()->findAllByAttributes(array(
			'list_id'=>outBin($listId)
		),array('order'=>'time desc'));
		$this->render('add',array(
			'list'=>$list,
			'model'=>$model,
			'dataGoals'=>$dataGoals,
		));
	}
	public function actionCreateList(){
		$model = new Lists;
		if(isset($_POST['ajax']) && $_POST['ajax']==='lists-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
		if(isset($_POST['Lists']))
		{
			$model->attributes=$_POST['Lists'];
			if($model->validate()){
				$model->time = time();
				$model->user_id = currentUser()->id;
				if($model->save()){
					Yii::app()->user->setFlash('success', 'Created new list successfully.');
					$this->redirect(array('/goals?uId='.outStr(currentUser()->id)));
				}else{
					$errors = $model->getErrors();
					list ($field, $_errors) = each ($errors);
					throw new Exception($_errors[0]);
				}
			}else{
				$errors = $model->getErrors();
				list ($field, $_errors) = each ($errors);
				throw new Exception($_errors[0]);
			}
		}
	}
	public function actionIndex($uId=NULL)
	{
		$this->layout = "publish";
		$user = Users::model()->findByPk(outBin($uId));
		if(empty($user)) $this->redirect(array('/site'));
		if($uId==NULL) $lists = Lists::model()->findAllByAttributes(array(),array('order'=>'time desc'));
		else $lists = Lists::model()->findAllByAttributes(array(
			'user_id'=>outBin($uId)
		),array('order'=>'time desc'));
		$this->render('my_goals',array(
			'lists'=>$lists,
			'uId'=>$uId,
		));
	}
	

	
}